package cn.jzq.mysoul.yourname.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author 蒋正强
 */
public class CommonUtil
{

    /**
     * 将String按separatorChar分割成Integer[]
     * @param str
     * @param separatorChar
     * @return
     */
    public static Integer[] stringToIntegerArr(final String str, final Character separatorChar)
    {
        Integer[] params = null;
        if (StringUtils.isNotBlank(str))
        {
            String[] stringArr = StringUtils.split(str, separatorChar);
                params = new Integer[stringArr.length];

            for (int i = 0; i < params.length; i++)
                params[i] = Integer.valueOf(stringArr[i]);
        }

        return params;
    }

    public static Long[] stringToLongArr(final String str, final Character separatorChar)
    {
        Long[] params = null;
        if (StringUtils.isNotBlank(str))
        {
            String[] stringArr = StringUtils.split(str, separatorChar);
            params = new Long[stringArr.length];

            for (int i = 0; i < params.length; i++)
                params[i] = Long.valueOf(stringArr[i]);
        }

        return params;
    }

    /**
     * Intger数组转String
     * @param arr
     * @param separator
     *      连接符
     * @return
     */
    public static String IntegerArrToString(Integer[] arr, Character separator)
    {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < arr.length; i++)
        {
            if (i < (arr.length - 1))
                sb.append(arr[i].toString() + separator);
            else
                sb.append(arr[i].toString());
        }


        return sb.toString();
    }


    /**
     * 根据list中的一个属性进行分类
     * @param list
     * @return
     */
    public static Map getListByKey(List list, Class clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
    {
        // 通过反射去拿

        if (list != null)
        {
            Constructor constructor = clazz.getDeclaredConstructor();
            constructor.setAccessible(true);
            Object obj = constructor.newInstance();

           // for (Object : )

        }

        /*
        Map map = new HashMap();
        for (int i = 0; list != null && i < list.size(); i++) {
            LifePaymentInfo obj = (LifePaymentInfo) list.get(i);
            String key = obj.getContNo();

            if (map.containsKey(key)) {
                List lists = (List) map.get(key);
                lists.add(obj);
            } else {
                List lists = new ArrayList();
                lists.add(obj);
                map.put(key, lists);
            }
        }
        return map;
    }
    /*

     */
        return null;
    }

}
